package com.masterrabbit.sn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.masterrabbit.common.core.web.domain.AjaxResult;
import com.masterrabbit.sn.domain.Activities;
import com.masterrabbit.sn.service.ActivitiesService;
import com.masterrabbit.sn.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/activities")
public class ActivitiesController {
    @Autowired
    private ActivitiesService activitiesService;
    /**
     * 添加活动
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@RequestBody Activities activities) {
        int result = activitiesService.addActivities(activities);
        if (result == 1) {
            return R.ok(null);
        } else {
            return R.error("插入失败");
        }
    }
    /**
     * 根据上架情况查询活动
     */
    @GetMapping("/offline/{merchantId}")
    public AjaxResult getOfflineActivities(
            @PathVariable Integer merchantId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "7") int pageSize) {
        Page<Activities> page = activitiesService.getOfflineActivities(merchantId, pageNum, pageSize);
        return AjaxResult.success(page);
    }
    /**
     * 根据上架情况查询活动
     */
    @GetMapping("/online/{merchantId}")
    public AjaxResult getOnlineActivities(
            @PathVariable Integer merchantId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "7") int pageSize) {
        Page<Activities> page = activitiesService.getOnlineActivities(merchantId, pageNum, pageSize);
        return AjaxResult.success(page);
    }
    /**
     * 查看活动详情
     */
    @GetMapping("/details/{activitiesId}")
    public AjaxResult getActivities(@PathVariable Integer activitiesId) {
        Activities activities = activitiesService.getActivitiesDetails(activitiesId);
        return AjaxResult.success(activities);
    }
    /**
     * 删除活动
     */
    @DeleteMapping("/delete/{activitiesId}")
    public AjaxResult deleteActivities(@PathVariable Integer activitiesId) {
        return activitiesService.deleteActivities(activitiesId) == 1 ?
                AjaxResult.success("删除成功") :
                AjaxResult.error("删除失败");
    }
    /**
     * 上架活动
     */
    @PutMapping("/online/{activitiesId}")
    public AjaxResult onlineActivities(@PathVariable Integer activitiesId) {
        return activitiesService.updateActivitiesOnline(activitiesId) == 1?
                AjaxResult.success("上架成功") :
                AjaxResult.error("上架失败");
    }
    /**
     * 下架活动
     */
    @PutMapping("/offline/{activitiesId}")
    public AjaxResult offlineActivities(@PathVariable Integer activitiesId) {
        return activitiesService.updateActivitiesOffline(activitiesId) == 1?
                AjaxResult.success("下架成功") :
                AjaxResult.error("下架失败");
    }

}
